const ifFullscreen = () => {
  return !!(
    document.fullscreen
    || document.mozFullScreen
    || document.webkitIsFullScreen
    || document.webkitFullScreen
    || document.msFullScreen
  )
}

let isFullscreen = ifFullscreen()

const requestFullscreen = mainEl => {
  if (mainEl.requestFullscreen) {
    mainEl.requestFullscreen();
  } else if (mainEl.mozRequestFullScreen) {
    mainEl.mozRequestFullScreen();
  } else if (mainEl.webkitRequestFullscreen) {
    mainEl.webkitRequestFullscreen();
  } else if (mainEl.msRequestFullscreen) {
    mainEl.msRequestFullscreen();
  }
  isFullscreen = true
}

const exitFullscreen = () => {
  if (document.exitFullscreen) {
    document.exitFullscreen()
  } else if (document.mozCancelFullScreen) {
    document.mozCancelFullScreen()
  } else if (document.webkitExitFullScreen) {
    document.webkitExitFullScreen()
  } else if (document.msExitFullScreen) {
    document.msExitFullScreen()
  }
  isFullscreen = false
}

const toggleFullscreen = (el) => {
  ifFullscreen() ? exitFullscreen() : requestFullscreen(el)

  return isFullscreen
}

export {
  ifFullscreen,
  requestFullscreen,
  exitFullscreen,
  toggleFullscreen,
}
